package com.yjy.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.system.comm.util.Snowflake;
import com.system.common.mapper.SysDeptMapper;
import com.system.common.mapper.SysMenuMapper;
import com.system.common.mapper.SysRoleMapper;
import com.system.common.mapper.SysRoleMenuMapper;
import com.system.common.mapper.SysRoleUserMapper;
import com.system.common.mapper.SysUserMapper;
import com.system.common.pojo.SysDept;
import com.system.common.pojo.SysDeptExample;
import com.system.common.pojo.SysMenu;
import com.system.common.pojo.SysMenuExample;
import com.system.common.pojo.SysRole;
import com.system.common.pojo.SysRoleExample;
import com.system.common.pojo.SysRoleExample.Criteria;
import com.system.common.pojo.SysRoleMenu;
import com.system.common.pojo.SysRoleMenuExample;
import com.system.common.pojo.SysRoleUser;
import com.system.common.pojo.SysRoleUserExample;
import com.system.common.pojo.SysUser;
@Service
public class SysRoleServiceImpl implements SysRoleService{
	
	@Autowired
	private SysRoleMapper roleMapper;
	
	@Autowired
	private SysRoleUserMapper ruMapper;
	
	@Autowired
	private SysDeptMapper deptMapper;
	
	@Autowired
	private SysUserMapper userMapper;
	
	@Autowired
	private SysRoleMenuMapper rmMapper;
	
	@Autowired
	private SysMenuMapper menuMapper;

	@Override
	public List<SysRole> listAll(Map<?, ?> map) {
		String roleName=(String)map.get("rname");
		SysRoleExample example=new SysRoleExample();
		Criteria cri=example.createCriteria();
		if (StringUtils.isNotBlank(roleName)) {
			cri.andRoleNameLike(roleName);
		}			
		return roleMapper.selectByExample(example);
	}

	@Override
	public List<SysRole> selectRole(String userId) {
		List<SysRole> roles=new ArrayList<>();
		SysRoleUserExample example=new SysRoleUserExample();
		com.system.common.pojo.SysRoleUserExample.Criteria cri=example.createCriteria();
		cri.andUserIdEqualTo(userId);
		List<SysRoleUser> list=ruMapper.selectByExample(example);
		for (SysRoleUser ru : list) {
			SysRole role=roleMapper.selectByPrimaryKey(ru.getRoleId());
			roles.add(role);
		}
		return roles;
	}

	@Override
	public void delete(String rid) {
		roleMapper.deleteByPrimaryKey(rid);
	}

	@Override
	public boolean checkName(String roleName) {
		SysRole role=roleMapper.checkName(roleName);
		if (role!=null) {
			return false;
		}
		return true;
	}

	@Override
	public void update(SysRole role) {
		SysRole sysRole=roleMapper.selectByPrimaryKey(role.getRoleId());
		role.setCreateTime(sysRole.getCreateTime());
		role.setCreateUserId(sysRole.getCreateUserId());
		roleMapper.updateByPrimaryKey(role);		
	}

	@Override
	public void save(SysRole role) {
		role.setRoleId(new Snowflake(0, 0).nextId()+"");
		role.setCreateTime(new Date());
		roleMapper.insert(role);		
	}

	@Override
	public SysRole getById(String roleId) {
		return roleMapper.selectByPrimaryKey(roleId);
	}
	
	@Override
	public List<Map<String, Object>> nouserPower() {//没有当前角色的用户
		List<Map<String, Object>> sysUsers=userMapper.nouserPower();
		return sysUsers;
	}
	
	@Override
	public List<Map<String, Object>> yesUserPower(String roleId) {//有当前角色的用户
		List<Map<String, Object>> sysUsers=userMapper.yesUserPower(roleId);
		return sysUsers;
	}
	
	/*点击roleTree获取当前节点下面所有的用户信息*/
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public List<Map<String, Object>> clickZtree(String deptId) {
		List<Map<String, Object>>  sysUsers=new ArrayList<>();
		List<SysDept>  depts=new ArrayList<>();
	    SysDept dept=deptMapper.selectByPrimaryKey(deptId);
		depts= this.getTreeMenuList(depts, dept);
	    
	    List<Map<String, Object>>  users=userMapper.nouserPower();
		for (SysDept sysDept : depts) {//循环外面的所有的部门的节点
			for (Map<String, Object> map : users) {//全部都没有赋予角色的用户
				String deptID=(String) map.get("deptID");//获取里面的deptID
				if (sysDept.getDeptId().equals(deptID)) {//如果子节点匹配上了获取的deptId
					sysUsers.add(map);//就添加到null集合
				}
			}
		}
		return sysUsers;
	}
	public List<SysDept> getTreeMenuList(List<SysDept> domainList,SysDept dept) {		
	    domainList.add(dept);
	    SysDeptExample example=new SysDeptExample();
	    com.system.common.pojo.SysDeptExample.Criteria criteria= example.createCriteria();
	    criteria.andParentIdEqualTo(dept.getDeptId());
	    criteria.andIsDelEqualTo("0");
	    List<SysDept> depts=deptMapper.selectByExample(example);
	    if (!depts.isEmpty()) {
	      for(SysDept type : depts) {
	            getTreeMenuList(domainList,type);
	       }
		}
	    return domainList;
	}
	
	@Override
	public String pitchOn(String ids, String checkusers,HttpSession session) {//给用户赋角色
		String [] users=checkusers.split(",");
		SysUser user=(SysUser) session.getAttribute("user");
		for (String userId : users) {
			SysRoleUser roleUser=new SysRoleUser();
			roleUser.setRoleId(ids);
			roleUser.setUserId(userId);
			roleUser.setCreateUserId(user.getUserId());
			roleUser.setCreateTime(new Date());
			ruMapper.insert(roleUser);
		}
		return "员工角色赋值成功!";
	}
	
	@Override
	public String cancel(String ids, String checkusers) {//撤销用户角色
		String [] users=checkusers.split(",");
		for (String userId : users) {
			SysRoleUserExample example=new SysRoleUserExample();
			com.system.common.pojo.SysRoleUserExample.Criteria criteria=example.createCriteria();
			criteria.andUserIdEqualTo(userId);
			criteria.andRoleIdEqualTo(ids);
			ruMapper.deleteByExample(example);
		}
		return "角色删除权限成功!";
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public List<SysMenu> menuPower(String roleId) {//获取菜单树
		SysRoleMenuExample example = new SysRoleMenuExample();
		com.system.common.pojo.SysRoleMenuExample.Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		List<SysRoleMenu> roleMenus = rmMapper.selectByExample(example);
		SysMenuExample menuExample = new SysMenuExample();
		List<SysMenu> menus = menuMapper.selectByExample(menuExample);
		for (SysRoleMenu sysRoleMenu : roleMenus) {
			for (SysMenu sysMenu : menus) {
				if (sysRoleMenu.getMenuId().equals(sysMenu.getMenuId())) {
					sysMenu.setChecked(true);
				}
			}
		}
		return menus;
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void PowerRole(String menus, String roleIds, HttpSession session) {//给角色赋予菜单权限
		SysUser user = (SysUser) session.getAttribute("user");
		SysRoleMenuExample example = new SysRoleMenuExample();
		com.system.common.pojo.SysRoleMenuExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(roleIds)) {
				criteria.andRoleIdEqualTo(roleIds);
		}
		rmMapper.deleteByExample(example);
		String[] sysmenus = menus.split(",");
		for (String menuId : sysmenus) {
			SysRoleMenu roleMenu = new SysRoleMenu();
			roleMenu.setCreateTime(new Date());
			roleMenu.setCreateUserId(user.getUserId());
			roleMenu.setGrantType("1");
			roleMenu.setMenuId(menuId);
			roleMenu.setRoleId(roleIds);
			rmMapper.insert(roleMenu);
		}
	}

	@Override
	public List<SysMenu> selectPower(String userId) {
		SysRoleUser roleUser=ruMapper.selectRole(userId);
		List<SysMenu> list=menuMapper.selectPower(roleUser.getRoleId());
		return list;
	}
}
